package org.hfchunya.mutualHelp.modular.system.controller;

import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import org.hfchunya.mutualHelp.config.properties.GunsProperties;
import org.hfchunya.mutualHelp.core.common.annotion.Permission;
import org.hfchunya.mutualHelp.core.common.exception.BizExceptionEnum;
import org.hfchunya.mutualHelp.core.common.page.LayuiPageInfo;
import org.hfchunya.mutualHelp.modular.system.entity.TeamMembers;
import org.hfchunya.mutualHelp.modular.system.service.TeamMembersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;


/**
 * 控制器
 *
 * @author 杨凯程
 * @Date 2019-05-08 21:47:18
 */
@Controller
@RequestMapping("/teamMembers")
public class TeamMembersController extends BaseController {

    private String PREFIX = "modular/system/teamMembers";

    @Autowired
    private TeamMembersService teamMembersService;

    @Autowired
    private GunsProperties gunsProperties;

    /**
     * 跳转到主页面
     *
     * @author 杨凯程
     * @Date 2019-05-08
     */
    @RequestMapping("")
    public String index() {
        return PREFIX+"/teamMembers.html";
    }

    /**
     * 新增页面
     *
     * @author 杨凯程
     * @Date 2019-05-08
     */
    @Permission
    @RequestMapping("/add")
    public String add() {

        return PREFIX + "/teamMembers_add.html";
    }

    /**
     * 编辑页面
     *
     * @author 杨凯程
     * @Date 2019-05-08
     */
    @Permission
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/teamMembers_edit.html";
    }

    /**
     * 新增接口
     *
     * @author 杨凯程
     * @Date 2019-05-08
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(TeamMembers teamMembersParam) {
        this.teamMembersService.insert(teamMembersParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author 杨凯程
     * @Date 2019-05-08
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(TeamMembers teamMembersParam) {
        this.teamMembersService.updateByPrimaryKeySelective(teamMembersParam);
        return ResponseData.success();
    }
    /**
     * 删除接口
     *
     * @author 杨凯程
     * @Date 2019-05-08
     */
    @Permission
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(TeamMembers teamMembersParam) {
        this.teamMembersService.deleteByPrimaryKey(teamMembersParam.getId());
        return ResponseData.success();
    }

    /**
     * 查询列表
     *
     * @author 杨凯程
     * @Date 2019-05-08
     */
    @ResponseBody
    @RequestMapping("/list")
    public  LayuiPageInfo list(TeamMembers r) {
        List<TeamMembers> list = teamMembersService.selectlist(r);
        LayuiPageInfo layuiPageInfo = new LayuiPageInfo();
        layuiPageInfo.setData(list);
        return layuiPageInfo;
    }
    /**
     * 查询列表
     *
     * @author 杨凯程
     * @Date 2019-05-08
     */
    @ResponseBody
    @RequestMapping("/detail/{id}")
    public HashMap<String,TeamMembers> detail(@PathVariable("id")  int id) {
        TeamMembers teamMembe = teamMembersService.selectByPrimaryKey(id);
        HashMap<String,TeamMembers> map = new HashMap<String,TeamMembers>();
        map.put("data",teamMembe);
        return map;
    }

    /**
     * 上传图片
     *
     * @author fengshuonan
     * @Date 2018/12/24 22:44
     */
    @RequestMapping(method = RequestMethod.POST, path = "/upload")
    @ResponseBody
    public LayuiPageInfo upload(@RequestPart("file") MultipartFile picture) {

        String pictureName = UUID.randomUUID().toString() + "." + ToolUtil.getFileSuffix(picture.getOriginalFilename());
        LayuiPageInfo layuiPageInfo = new LayuiPageInfo();
        try {
            String fileSavePath = gunsProperties.getFileUploadPath();
            picture.transferTo(new File(fileSavePath + pictureName));
            List list = new ArrayList();
            list.add("/assets/common/cytximag/"+pictureName);
            layuiPageInfo.setData(list);
        } catch (Exception e) {
            throw new ServiceException(BizExceptionEnum.UPLOAD_ERROR);
        }
        return layuiPageInfo;
    }
}


